using System;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;

namespace StudentManagementApplication.DAO
{
    public class CourseDAO : BaseDAO
    {
        public CourseDAO()
        {
        }

        private Entities.Course CreateCourseFromReader(IDataReader reader)
        {
            Entities.Course course = new Entities.Course();
            course.CourseID = (string)reader["cCourseID"];
            course.CourseName = (string)reader["vCourseName"];
            return course;
        }

        public void InsertCourse(Entities.Course course)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("prcInsertCourse", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@vCourseName", course.CourseName));
                connection.Open();
                command.ExecuteNonQuery();
                //if (command.ExecuteNonQuery() <= 0)
                //    throw;
            }
        }

        public void UpdateCourse(Entities.Course course)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("prcUpdateCourse", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@cCourseID", course.CourseID));
                command.Parameters.Add(new SqlParameter("@vCourseName", course.CourseName));
                connection.Open();
                command.ExecuteNonQuery();
                //if (command.ExecuteNonQuery() <= 0)
                //    throw;
            }
        }

        public void DeleteCourse(String sCourseID)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("prcDeleteCourse", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@cCourseID", sCourseID));
                connection.Open();
                command.ExecuteNonQuery();
                //if (command.ExecuteNonQuery() <= 0)
                //    throw;
            }
        }

        public Entities.Course GetCourseByCourseID(string sCourseID)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("prcGetCourseByCourseID", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("cCourseID", sCourseID));

                connection.Open();
                Entities.Course course = null;
                using (IDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    if (reader.Read())
                    {
                        course = CreateCourseFromReader(reader);
                    }
                    //else
                    //    throw;
                }
                return course;
            }
        }

        public List<Entities.Course> GetAllCourses()
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("prcGetAllCourses", connection);
                command.CommandType = CommandType.StoredProcedure;

                connection.Open();
                List<Entities.Course> courses = new List<Entities.Course>();
                using (IDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    while (reader.Read())
                    {
                        courses.Add(CreateCourseFromReader(reader));
                    }
                }
                return courses;
            }
        }


    }
}
